package com.itheima.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/*")
public class FilterDemo implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //System.out.println("1.过滤器执行...");

        /*HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        if(session.getAttribute("user") != null){
            //放行
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            //拦截
            request.setAttribute("login_msg","你尚未登录");
            request.getRequestDispatcher("/login.jsp").forward(request,servletResponse);
        }*/

        //放行
        filterChain.doFilter(servletRequest,servletResponse);
        //System.out.println("3.过滤器执行...");
    }

    @Override
    public void destroy() {

    }
}
